package com.qeegoo.b2bautozityp.moudle.login.viewmodel;

import android.databinding.ObservableField;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.BuildConfig;
import com.qeegoo.b2bautozityp.SelectHostActivity;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.moudle.home.view.MainActivity;
import com.qeegoo.b2bautozityp.moudle.login.model.LoginBean;
import com.qeegoo.b2bautozityp.moudle.login.view.RegisterActivity;

import base.lib.util.ActivityManager;
import base.lib.util.NavigateUtils;
import base.lib.util.PreferencesUtils;

/**
 * Created by lining on 2017/9/15.
 */

public class LoginViewModel {

    private RequestApi mRequestApi;
    public ObservableField<Boolean> check = new ObservableField<>(false);//密码是否可见
    public ObservableField<String> account = new ObservableField<>(PreferencesUtils.getString(Constants.sUser_account, ""));//用户名
    public ObservableField<String> pwd = new ObservableField<>("");//密码
    public ObservableField<String> error = new ObservableField<>("");//错误提醒
    public LoginViewModel(RequestApi requestApi) {
        mRequestApi = requestApi;
    }

    //清空用户名密码
    public ReplyCommand accountClearCommand = new ReplyCommand<>(() -> account.set(""));

    //登录
    public ReplyCommand loginCommand = new ReplyCommand(() -> {
        mRequestApi.login(HttpParams.userLogin(account.get().toString(),pwd.get().toString()))
                .compose(RetrofitService.applySchedulers())
                .subscribe(new ProgressSubscriber<LoginBean>(){
                    @Override
                    public void onNext(LoginBean loginBean) {
                        if(loginBean.status.isSuccess()){
                            PreferencesUtils.saveString(Constants.sUser_token,loginBean.data.token);
                            PreferencesUtils.saveString(Constants.sUser_partyId,loginBean.data.partyId);
                            PreferencesUtils.saveString(Constants.sUser_partyName,loginBean.data.partyName);
                            PreferencesUtils.saveString(Constants.sUser_username,loginBean.data.username);
                            NavigateUtils.startActivity(MainActivity.class);
                            ActivityManager.getActivity().finish();
                        }else {
                            error.set(loginBean.status.msg);
                        }

                    }
                });
//        mRequestApi.Login(HttpParams.login(account.get(),pwd.get()))
//                .map(new RetrofitService.HttpResultFunc<>())
//                .compose(RetrofitService.applySchedulers())
//                .subscribe(new ProgressSubscriber<LoginBean>() {
//                    @Override
//                    public void onNext(LoginBean loginBean) {
//                        PreferencesUtils.saveString(Constants.sUser_token,loginBean.token);
//                        PreferencesUtils.saveString(Constants.sUser_partyName,loginBean.partyName);
//                        PreferencesUtils.saveString(Constants.sUser_partyId,loginBean.partyId);
//                        PreferencesUtils.saveString(Constants.sUser_username,loginBean.username);
//                        PreferencesUtils.saveString(Constants.sUser_userId,loginBean.userId);
//                        PreferencesUtils.saveString(Constants.sUser_phone,loginBean.phone);
//                        PreferencesUtils.saveString(Constants.sUser_email,loginBean.email);
//                        PreferencesUtils.saveBoolean(Constants.sUser_loginFlag, true);
//                        PreferencesUtils.saveString(Constants.sUser_account,account.get());
//
//                        NavigateUtils.startActivity(HomeActivity.class);
//                        ActivityManager.getActivity().finish();
//                    }
//                });
//        ToastUtils.showToast("登录");
    });

    //立即注册
    public ReplyCommand registerCommand = new ReplyCommand(() -> {
        NavigateUtils.startActivity(RegisterActivity.class);
//        ToastUtils.showToast("立即注册");
    });

    public ObservableField<Boolean> isDebug = new ObservableField<>(BuildConfig.DEBUG);
    public ReplyCommand changeServer = new ReplyCommand(() -> {
        NavigateUtils.startActivityForResult(SelectHostActivity.class, 1000);
    });
    public ObservableField<Boolean> delete = new ObservableField<>(account.get().length() > 0);

}
